package com.lyb.ui.controller;

import com.lyb.constant.SystemConstant;
import com.lyb.hardware.AlpacaPCB;
import com.lyb.hardware.io.A;
import com.lyb.hardware.io.B;
import com.lyb.hardware.io.C;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.util.Duration;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.ResourceBundle;

/**
 * 设备页面的控制器
 *
 * @author Dayun Mr.Alpaca
 * @version 1.4.1
 */
public class DeviceController implements Initializable {
    @FXML
    public AnchorPane mainPane;
    @FXML
    public ImageView aImage;
    @FXML
    public Label aSign;
    @FXML
    public ImageView bImage;
    @FXML
    public Label bSign;
    @FXML
    public ImageView cImage;
    @FXML
    public Label cSign;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        /* 获取borderPane里面的对象 */
        ObservableList<Node> borderPanes = mainPane.getChildren();
        /* 获取三个设备的图片 */
        List<ImageView> pics = new ArrayList<>();
        for (Node node : borderPanes) {
            BorderPane borderPane = (BorderPane) node;
            ImageView curImageView = (ImageView) borderPane.getCenter();
            pics.add(curImageView);
        }

        /* 创建Image图像 */
        Image unuse = new Image("/pics/device_unuse.png");
        Image aUse = new Image("/pics/device_red.png");
        Image bUse = new Image("/pics/device_blue.png");
        Image cUse = new Image("/pics/device_green.png");

        Timeline timeline = new Timeline();
        KeyFrame keyFrame = new KeyFrame(Duration.millis(500), event -> {
            Queue<A> aQueue = SystemConstant.MAIN_BOARD.getaQueueFromDma();
            Queue<B> bQueue = SystemConstant.MAIN_BOARD.getbQueueFromDma();
            Queue<C> cQueue = SystemConstant.MAIN_BOARD.getcQueueFromDma();
            Image aImage = aQueue.size() == SystemConstant.DEVICE_A_NUM ? unuse : aUse;
            Image bImage = bQueue.size() == SystemConstant.DEVICE_B_NUM ? unuse : bUse;
            Image cImage = cQueue.size() == SystemConstant.DEVICE_C_NUM ? unuse : cUse;
            pics.get(0).setImage(aImage);
            pics.get(1).setImage(bImage);
            pics.get(2).setImage(cImage);
        });
        timeline.getKeyFrames().add(keyFrame);
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
        timeline.play();
    }
}


